import {Component, OnDestroy, OnInit} from "@angular/core";
import {GameApi} from "../../../api/GameApi";
import {Game} from "../gameScript/tankFighter/core/Game";
import {Urls} from "../../../api/ApiUrl";
import {Utils} from "../gameScript/tankFighter/utils/Utils";
import {ActivatedRoute, Router} from "@angular/router";
import { FullscreenModule } from 'ng-devui/fullscreen';

@Component({
  selector: 'game-engine',
  templateUrl: './game-engine.component.html',
  styleUrls: ['./game-engine.component.scss']
})
export class GameEngineComponent implements OnInit ,OnDestroy{

  route: ActivatedRoute
  router: Router
  game: Game | undefined
  constructor(route: ActivatedRoute, router: Router) {
    this.route = route;
    this.router = router;
  }

  async ngOnInit() {
    await this.tryStartGame()
  }

  async tryStartGame() {
    let result = await GameApi.getGameId();
    if (result.success()) {
      const elementById = document.getElementById('gameCanvas') as HTMLCanvasElement;
      this.game = new Game(Urls.game.websocket, result.data!, elementById,() => {
      })
       this.game.start()
    } else {
      result.resolveError(this.router, this.route)
      await Utils.sleep(1000)
      await this.tryStartGame()
    }
  }


  ngOnDestroy(): void {
    this.game?.stopGame()
  }
  btnIcon = 'icon-frame-expand';
  btnContent = '全屏';


}
